package com.xydtech.busiflw.service.impl;

import com.xydtech.busiflw.domain.BusiflWaitTask;
import com.xydtech.busiflw.mapper.BusiflInstStepMapper;
import com.xydtech.busiflw.mapper.BusiflInstanceMapper;
import com.xydtech.busiflw.mapper.BusiflWaitTaskMapper;
import com.xydtech.busiflw.service.IBusiflWaitTaskService;
import com.xydtech.common.core.text.Convert;
import com.xydtech.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 待办任务Service业务层处理
 *
 * @author JHON
 * @date 2021-06-11
 */
@Service
public class BusiflWaitTaskServiceImpl implements IBusiflWaitTaskService {
    @Autowired
    private BusiflWaitTaskMapper busiflWaitTaskMapper;
    @Autowired
    private BusiflInstanceMapper busiflInstanceMapper;
    @Autowired
    private BusiflInstStepMapper busiflInstStepMapper;

    /**
     * 查询待办任务
     *
     * @param taskId 待办任务ID
     * @return 待办任务
     */
    @Override
    public BusiflWaitTask selectBusiflWaitTaskById(Long taskId) {
        return busiflWaitTaskMapper.selectBusiflWaitTaskById(taskId);
    }

    /**
     * 查询待办任务列表
     *
     * @param busiflWaitTask 待办任务
     * @return 待办任务
     */
    @Override
    public List<BusiflWaitTask> selectBusiflWaitTaskList(BusiflWaitTask busiflWaitTask) {
        return busiflWaitTaskMapper.selectBusiflWaitTaskList(busiflWaitTask);
    }

    /**
     * 查询已提交任务列表
     *
     * @param busiflWaitTask 待办任务
     * @return 待办任务集合
     */
    @Override
    public List<BusiflWaitTask> selectHasSubmitTaskList(BusiflWaitTask busiflWaitTask) {
        return busiflWaitTaskMapper.selectHasSubmitTaskList(busiflWaitTask);
    }

    /**
     * 查询已结束任务列表
     *
     * @param busiflWaitTask 待办任务
     * @return 待办任务集合
     */
    @Override
    public List<BusiflWaitTask> selectCloseTaskList(BusiflWaitTask busiflWaitTask) {
        return busiflWaitTaskMapper.selectCloseTaskList(busiflWaitTask);
    }

    /**
     * 新增待办任务
     *
     * @param busiflWaitTask 待办任务
     * @return 结果
     */
    @Override
    public int insertBusiflWaitTask(BusiflWaitTask busiflWaitTask) {
        busiflWaitTask.setCreateTime(DateUtils.getNowDate());
        return busiflWaitTaskMapper.insertBusiflWaitTask(busiflWaitTask);
    }

    /**
     * 修改待办任务
     *
     * @param busiflWaitTask 待办任务
     * @return 结果
     */
    @Override
    public int updateBusiflWaitTask(BusiflWaitTask busiflWaitTask) {
        return busiflWaitTaskMapper.updateBusiflWaitTask(busiflWaitTask);
    }

    /**
     *修改其他人的任务
     * @Title: updateOtherUserBusiflWaitTask
     * @param busiflWaitTask
     * @return int
     * @exception
     * @author JHON
     * @Date 2021/11/27 16:56
     **/
    @Override
    public int updateOtherUserBusiflWaitTask(BusiflWaitTask busiflWaitTask) {
        return busiflWaitTaskMapper.updateOtherUserBusiflWaitTask(busiflWaitTask);
    }

    /**
     * 删除待办任务对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteBusiflWaitTaskByIds(String ids) {
        return busiflWaitTaskMapper.deleteBusiflWaitTaskByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除待办任务信息
     *
     * @param taskId 待办任务ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBusiflWaitTaskById(Long taskId) {
        BusiflWaitTask busiflWaitTask = busiflWaitTaskMapper.selectBusiflWaitTaskById(taskId);
        busiflInstanceMapper.deleteBusiflInstanceById(busiflWaitTask.getInstanceId());
        busiflInstStepMapper.deleteBusiflInstStepByInstanceId(busiflWaitTask.getInstanceId());
        return busiflWaitTaskMapper.deleteBusiflWaitTaskById(taskId);
    }
}
